package com.quanquan.user.service.statisticsReport;

import com.quanquan.user.entity.SelfHelpIntegral;
import com.quanquan.user.repository.SelfHelpIntegralRepository;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 功能描述:
 *
 * @ClassName: SelfHelpIntegralStatisticsReportService
 * @Author: xiaoqianh丶
 * @Date: 2019-05-29 16:09
 * @Version: V1.0
 */
@Service
public class SelfHelpIntegralStatisticsReportService {

    private final SelfHelpIntegralRepository selfHelpIntegralRepository;

    public SelfHelpIntegralStatisticsReportService(SelfHelpIntegralRepository selfHelpIntegralRepository) {
        this.selfHelpIntegralRepository = selfHelpIntegralRepository;
    }


    /**
     * 按时间查询（报表使用）（获得）
     * @param startTime
     * @param endTime
     * @return
     */
    public List<SelfHelpIntegral> statisticalReportsReceive(Date startTime, Date endTime) {
        return selfHelpIntegralRepository.findAll((Specification<SelfHelpIntegral>) (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();

            predicates.add(cb.greaterThanOrEqualTo(root.get("createTime").as(Date.class),startTime));
            predicates.add(cb.lessThanOrEqualTo(root.get("createTime").as(Date.class), endTime));
            predicates.add(cb.equal(root.get("applicationStatus").as(SelfHelpIntegral.Application.class), SelfHelpIntegral.Application.successpass));
            return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
        });
    }

    public Double statisticalReportsMoneyYear(Date startTime, Date endTime) {

        return selfHelpIntegralRepository.findMoneySelfHelpByNum(startTime, endTime);
    }



    public Double statisticalReportsReceiveYear(Date startTime, Date endTime) {

        return selfHelpIntegralRepository.findIntegralSelfHelpByNum(startTime, endTime);
    }
}
